﻿using System;
using System.Linq;
// ColorPicker, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace ColorPicker
{
    public class ColorNameWithSource
    {
        public string Name { get; set; }
        public string Source { get; set; }
    }
    public class ColorsCooked
    {
        public ColorNameWithSource[] ColorNames { get; set; }
        public byte ColorR { get; set; }
        public byte ColorG { get; set; }
        public byte ColorB { get; set; }
        public int ColorA { get; set; }
        //
        public uint ColorRGB { get; set; }
        public bool Done { get; set; }
        public double Distance { get; set; }
        //
        public ColorsCooked()
        {
        }

        public ColorsCooked(ColorNameWithSource[] Name, byte ColorR, byte ColorG, byte ColorB)
        {
            this.ColorNames = Name;
            this.ColorR = ColorR;
            this.ColorG = ColorG;
            this.ColorB = ColorB;
            this.ColorA = 0;
            //
            this.ColorRGB = ColorR; this.ColorRGB = this.ColorRGB << 8;
            this.ColorRGB |= ColorG; this.ColorRGB = this.ColorRGB << 8;
            this.ColorRGB |= ColorB;
        }
        public ColorsCooked(ColorNameWithSource[] Name, byte ColorR, byte ColorG, byte ColorB, uint ColorRGB)
        {
            this.ColorNames = Name;
            this.ColorR = ColorR;
            this.ColorG = ColorG;
            this.ColorB = ColorB;
            //
            this.ColorRGB = ColorRGB;
        }
        public ColorNameWithSource GetWinColor()
        {
            ColorNameWithSource x = (from c in this.ColorNames where c.Source == "+" || c.Source == "W" select c).FirstOrDefault();
            if (null != x) return x;
            return this.ColorNames[0];
        }
        public static bool IsSameColor(ColorsCooked a, ColorsCooked b)
        {
            return a.ColorR == b.ColorR && a.ColorG == b.ColorG && a.ColorB == b.ColorB;
        }

        public double GetDistance(ColorsCooked b)
        {
            return Math.Round(Math.Sqrt(Math.Pow(this.ColorR - b.ColorR, 2.0) + Math.Pow(this.ColorB - b.ColorB, 2.0) + Math.Pow(this.ColorG - b.ColorG, 2.0)), 1);
        }
    }
}
